using System;
using System.Collections;
using System.Web.UI.WebControls;

namespace Sedna.Core.Controller.Configurations
{
	public class DataGridColumnConfiguration : ClientScriptingConfiguration, IDataGridColumnConfiguration
	{

		private string dataField;
        private bool isItemIndexColumn;
        private bool isInvalidSighColumn;
        //		private IDataTranslator columnDataTranslator;
        private IDataGridInnerControlConfigurator controlConfigurator;
		private IDataGridCommandArgumentsGenerator commandArgumentsGenerator;
		private string commandName;
		private bool includeSource = false;
		private IConfiguration controlConfiguration;
		private IDataGridColumnInboundControlIdGenerator controlIdGenerator = null;
		private IConfiguration headerControlConfiguration = null;
		private bool sortable = false;
		private string sortExpression = null;
		private bool displaySortMarker = true;
		private bool isDefaultSort = false;
		private bool defaultSortAsc = true;
		private IDataGridColumnStyle headerColumnStyle;
		private IDataGridColumnStyle itemColumnStyle;
		private IList operationsVisible;
		private IDataGridColumnControlEnabler columnControlEnabler = new FakeEnabler();

		private string onClick;
	    private IImageConfiguration imageConfiguration;

	    public string OnClick
		{
			get { return onClick; }
			set { onClick = value; }
		}

//		private Unit width;

		public DataGridColumnConfiguration()
		{
		}

/*
		public Unit Width
		{
			get { return width; }
			set { width = value; }
		}
*/

		public string DataField
		{
			get { return dataField; }
			set { dataField = value; }
		}

/*
		public IDataTranslator ColumnDataTranslator
		{
			get { return columnDataTranslator; }
			set { columnDataTranslator = value; }
		}
*/


	    public IDataGridInnerControlConfigurator ControlConfigurator
	    {
	        get { return controlConfigurator; }
	        set { controlConfigurator = value; }
	    }

	    public IDataGridCommandArgumentsGenerator CommandArgumentsGenerator
		{
			get { return commandArgumentsGenerator; }
			set { commandArgumentsGenerator = value; }
		}

		public string CommandName
		{
			get { return commandName; }
			set { commandName = value; }
		}

		public bool IncludeSource
		{
			get { return includeSource; }
			set { includeSource = value; }
		}

		public IConfiguration ControlConfiguration
		{
			get { return controlConfiguration; }
			set { controlConfiguration = value; }
		}

		public IDataGridColumnInboundControlIdGenerator ControlIdGenerator
		{
			get { return controlIdGenerator; }
			set { controlIdGenerator = value; }
		}

		public IConfiguration HeaderControlConfiguration
		{
			get { return headerControlConfiguration; }
			set { headerControlConfiguration = value; }
		}

		public bool Sortable
		{
			get { return sortable; }
			set { sortable = value; }
		}

		public string SortExpression
		{
			get { return sortExpression ?? dataField; }
			set{ sortExpression = value; }
		}

		public bool DisplaySortMarker
		{
			get { return displaySortMarker; }
            set { displaySortMarker = value; }
		}

		public bool IsDefaultSort
		{
			get { return isDefaultSort; }
			set{ isDefaultSort = value; }
		}

		public bool DefaultSortAsc
		{
			get { return defaultSortAsc; }
			set
			{
				this.IsDefaultSort = true;
				defaultSortAsc = value;
			}
		}


		public IDataGridColumnStyle HeaderColumnStyle
		{
			get { return headerColumnStyle; }
			set { headerColumnStyle = value; }
		}

		public IDataGridColumnStyle ItemColumnStyle
		{
			get { return itemColumnStyle; }
			set { itemColumnStyle = value; }
		}


		public IList OperationsVisible
		{
			get { return operationsVisible; }
			set { operationsVisible = value; }
		}


		public IDataGridColumnControlEnabler ColumnControlEnabler
		{
			get { return columnControlEnabler; }
			set { columnControlEnabler = value; }
		}

        public bool IsItemIndexColumn
        {
            get { return isItemIndexColumn; }
            set{ isItemIndexColumn = value; }
        }

		public void ApplyStyles(DataGridColumn column)
		{
			if(headerColumnStyle != null)
			{
				headerColumnStyle.ApplyTableItemStyle(column.HeaderStyle);
			}
			if(itemColumnStyle != null)
			{
				itemColumnStyle.ApplyTableItemStyle(column.ItemStyle);
			}
		}


		public bool IsVisibleForOperation(string operation)
		{
			if(operationsVisible == null || operationsVisible.Count == 0)
			{
				return true;
			}
			if(operation == null || string.Empty == operation)
			{
				return false;
			}

			return operationsVisible.Contains(operation);
		}


		public class FakeEnabler : IDataGridColumnControlEnabler
		{
            public bool CheckIfEnabled(IDataGridColumnConfiguration columnConfiguration, object dataRow, IExecutionContext executionContext)
			{
				return columnConfiguration.ControlConfiguration.Enabled;
			}

            public bool CheckIfVisible(IDataGridColumnConfiguration columnConfiguration, object dataRow, IExecutionContext executionContext)
			{
				return true;
			}
		}

        public bool IsInvalidSighColumn
        {
            get { return isInvalidSighColumn; }
            set { isInvalidSighColumn = value; }
        }

	    public IImageConfiguration ImageConfiguration
	    {
	        set { imageConfiguration = value; }
	        get { return imageConfiguration; }
	    }

	}
}
